setuptools_build.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import sys
  2. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  3. if MYPY_CHECK_RUNNING:
  4. from typing import List, Optional, Sequence
  5. # Shim to wrap setup.py invocation with setuptools
  6. #
  7. # We set sys.argv[0] to the path to the underlying setup.py file so
  8. # setuptools / distutils don't take the path to the setup.py to be "-c" when
  9. # invoking via the shim. This avoids e.g. the following manifest_maker
  10. # warning: "warning: manifest_maker: standard file '-c' not found".
  11. _SETUPTOOLS_SHIM = (
  12. "import sys, setuptools, tokenize; sys.argv[0] = {0!r}; __file__={0!r};"
  13. "f=getattr(tokenize, 'open', open)(__file__);"
  14. "code=f.read().replace('\\r\\n', '\\n');"
  15. "f.close();"
  16. "exec(compile(code, __file__, 'exec'))"
  17. )
  18. def make_setuptools_shim_args(
  19. setup_py_path, # type: str
  20. global_options=None, # type: Sequence[str]
  21. no_user_config=False, # type: bool
  22. unbuffered_output=False # type: bool
  23. ):
  24. # type: (...) -> List[str]
  25. """
  26. Get setuptools command arguments with shim wrapped setup file invocation.
  27. :param setup_py_path: The path to setup.py to be wrapped.
  28. :param global_options: Additional global options.
  29. :param no_user_config: If True, disables personal user configuration.
  30. :param unbuffered_output: If True, adds the unbuffered switch to the
  31. argument list.
  32. """
  33. args = [sys.executable]
  34. if unbuffered_output:
  35. args += ["-u"]
  36. args += ["-c", _SETUPTOOLS_SHIM.format(setup_py_path)]
  37. if global_options:
  38. args += global_options
  39. if no_user_config:
  40. args += ["--no-user-cfg"]
  41. return args
  42. def make_setuptools_bdist_wheel_args(
  43. setup_py_path, # type: str
  44. global_options, # type: Sequence[str]
  45. build_options, # type: Sequence[str]
  46. destination_dir, # type: str
  47. ):
  48. # type: (...) -> List[str]
  49. # NOTE: Eventually, we'd want to also -S to the flags here, when we're
  50. # isolating. Currently, it breaks Python in virtualenvs, because it
  51. # relies on site.py to find parts of the standard library outside the
  52. # virtualenv.
  53. args = make_setuptools_shim_args(
  54. setup_py_path,
  55. global_options=global_options,
  56. unbuffered_output=True
  57. )
  58. args += ["bdist_wheel", "-d", destination_dir]
  59. args += build_options
  60. return args
  61. def make_setuptools_clean_args(
  62. setup_py_path, # type: str
  63. global_options, # type: Sequence[str]
  64. ):
  65. # type: (...) -> List[str]
  66. args = make_setuptools_shim_args(
  67. setup_py_path,
  68. global_options=global_options,
  69. unbuffered_output=True
  70. )
  71. args += ["clean", "--all"]
  72. return args
  73. def make_setuptools_develop_args(
  74. setup_py_path, # type: str
  75. global_options, # type: Sequence[str]
  76. install_options, # type: Sequence[str]
  77. no_user_config, # type: bool
  78. prefix, # type: Optional[str]
  79. home, # type: Optional[str]
  80. use_user_site, # type: bool
  81. ):
  82. # type: (...) -> List[str]
  83. assert not (use_user_site and prefix)
  84. args = make_setuptools_shim_args(
  85. setup_py_path,
  86. global_options=global_options,
  87. no_user_config=no_user_config,
  88. )
  89. args += ["develop", "--no-deps"]
  90. args += install_options
  91. if prefix:
  92. args += ["--prefix", prefix]
  93. if home is not None:
  94. args += ["--home", home]
  95. if use_user_site:
  96. args += ["--user", "--prefix="]
  97. return args
  98. def make_setuptools_egg_info_args(
  99. setup_py_path, # type: str
  100. egg_info_dir, # type: Optional[str]
  101. no_user_config, # type: bool
  102. ):
  103. # type: (...) -> List[str]
  104. args = make_setuptools_shim_args(
  105. setup_py_path, no_user_config=no_user_config
  106. )
  107. args += ["egg_info"]
  108. if egg_info_dir:
  109. args += ["--egg-base", egg_info_dir]
  110. return args
  111. def make_setuptools_install_args(
  112. setup_py_path, # type: str
  113. global_options, # type: Sequence[str]
  114. install_options, # type: Sequence[str]
  115. record_filename, # type: str
  116. root, # type: Optional[str]
  117. prefix, # type: Optional[str]
  118. header_dir, # type: Optional[str]
  119. home, # type: Optional[str]
  120. use_user_site, # type: bool
  121. no_user_config, # type: bool
  122. pycompile # type: bool
  123. ):
  124. # type: (...) -> List[str]
  125. assert not (use_user_site and prefix)
  126. assert not (use_user_site and root)
  127. args = make_setuptools_shim_args(
  128. setup_py_path,
  129. global_options=global_options,
  130. no_user_config=no_user_config,
  131. unbuffered_output=True
  132. )
  133. args += ["install", "--record", record_filename]
  134. args += ["--single-version-externally-managed"]
  135. if root is not None:
  136. args += ["--root", root]
  137. if prefix is not None:
  138. args += ["--prefix", prefix]
  139. if home is not None:
  140. args += ["--home", home]
  141. if use_user_site:
  142. args += ["--user", "--prefix="]
  143. if pycompile:
  144. args += ["--compile"]
  145. else:
  146. args += ["--no-compile"]
  147. if header_dir:
  148. args += ["--install-headers", header_dir]
  149. args += install_options
  150. return args